package com.ymh.common.domain;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

/**
 * @author: 13590
 * @date: 2020/10/27 18:28
 * @description:
 */
@NoArgsConstructor
@Data
@Accessors(chain = true)
public class ResultData<T> {

  /**
   * 结果状态 ,正常响应200，其他状态码都为失败
   */
  private int status;
  private String message;
  private T data;
  private boolean success;
  private long timestamp;


  public static <T> ResultData<T> ok(T data) {
    return new ResultData<T>().setStatus(200).setData(data).setMessage("")
        .setSuccess(true).setTimestamp(
            LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")));
  }
  public static <T> ResultData<T> error(String msg) {
    return new ResultData<T>().setStatus(500).setMessage(msg)
        .setSuccess(false).setTimestamp(
            LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")));
  }


  public static <T> ResultData<T> success(T data, String message) {
    return  new ResultData<T>().setStatus(200).setData(data).setMessage(message)
        .setSuccess(true).setTimestamp(
            LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")));
  }

  public static <T> ResultData<T> success(String message) {
    return success(null, message);
  }
}
